<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="list" style="display:flex ; text-align:center;">

    </div>
    <button class="prev">上一页</button>
    <button class="next">下一页</button>
    <span>1/4</span>

</body>

</html>
<script src="./promise.js"></script>
<script>
    var page = 1;


    function objToSearchString(obj) {
        var str = '';
        for (var key in obj) {
            str += `${key}=${obj[key]}&`
        }
        return str.slice(0, str.length - 1);
    }

    function ajax(method, url, params, cb) {
        var xhr = new XMLHttpRequest();
        xhr.open(method, url + "?" + objToSearchString(params));
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                cb(JSON.parse(xhr.responseText));
            }
        }
        xhr.send();
    }
    document.querySelector('.prev').onclick = function () {
        if (page > 1) {
            ajax('get', `https://yantianfeng.com/api/emotion`, {
                page: --page,
                count: 10
            }).than((data) => {
                var str = data.emotions.map((item, index) => {
                    return `<div> <img src="${item.url}" /> <div> ${item.title} </div> </div>`
                }).join('')
                document.querySelector('.list').innerHTML = str;
            })
        }
    }
    document.querySelector('.next').onclick = function () {
        if (page < 4) {
            ajax('get', `https://yantianfeng.com/api/emotion`, {
                page: ++page,
                count: 10
            }).than((data) => {
                var str = data.emotions.map((item, index) => {
                    return `<div> <img src="${item.url}" /> <div> ${item.title} </div> </div>`
                }).join('')
                document.querySelector('.list').innerHTML = str;
            })
        }
    }
</script>